//
//  GKStatusOriginalView.swift
//  MicroBlogFunc
//
//  Created by GK on 16/4/14.
//  Copyright © 2016年 GK. All rights reserved.
//

import UIKit

class GKStatusOriginalView: UIView {

    required init?(coder aDecoder: NSCoder)
    {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect)
    {
        super.init(frame: frame)
        backgroundColor = UIColor.whiteColor()
        prepareUI()
    }
    
    //添加控件
    private func prepareUI()
    {
        addSubview(iconView)
        addSubview(lblName)
        addSubview(lblTime)
        addSubview(lblSource)
        addSubview(memberShipView)
        addSubview(verifiedView)
        addSubview(lblContent)
        addSubview(picView)
        
        //添加约束
        iconView.snp_makeConstraints { (make) in
            make.leading.top.equalTo(self).offset(GKStatusCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        lblName.snp_makeConstraints { (make) in
            make.top.equalTo(iconView)
            make.leading.equalTo(iconView.snp_trailing).offset(GKStatusCellMargin)
        }
        lblTime.snp_makeConstraints { (make) in
            make.leading.equalTo(lblName)
            make.bottom.equalTo(iconView)
        }
        lblSource.snp_makeConstraints { (make) in
            make.leading.equalTo(lblTime.snp_trailing).offset(GKStatusCellMargin)
            make.bottom.equalTo(lblTime)
        }
        memberShipView.snp_makeConstraints { (make) in
            make.centerY.equalTo(lblName)
            make.leading.equalTo(lblName.snp_trailing).offset(GKStatusCellMargin)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        verifiedView.snp_makeConstraints { (make) in
            make.centerY.equalTo(iconView.snp_bottom)
            make.centerX.equalTo(iconView.snp_trailing)
            make.size.equalTo(CGSize(width: 17, height: 17))
        }
        lblContent.snp_makeConstraints { (make) in
            make.leading.equalTo(self).offset(GKStatusCellMargin)
            make.top.equalTo(iconView.snp_bottom).offset(GKStatusCellMargin)
            make.trailing.equalTo(self).offset(-GKStatusCellMargin)
        }
        picView.snp_makeConstraints { (make) in
            make.top.equalTo(lblContent.snp_bottom).offset(GKStatusCellMargin)
            make.leading.equalTo(self).offset(GKStatusCellMargin)
//            make.trailing.equalTo(self).offset(-GKStatusCellMargin)
            //TODO  先写死图片高度
//            make.height.equalTo(Int( arc4random_uniform(100) + 100) )
            make.size.equalTo(CGSizeZero)
        }
        
        
        ///// 自定义的原创微博view的底部和contentLabel的底部重合,偏移8
        self.snp_makeConstraints { (make) in
            make.bottom.equalTo(picView).offset(GKStatusCellMargin)
        }
    
    }
    
    //内容复制
    var status: GKStatus?
    {
        didSet
        {
            if let imgURLStr = status?.user?.profile_image_url
            {
                let  imgURL = NSURL(string: imgURLStr)
                iconView.sd_setImageWithURL(imgURL, placeholderImage: UIImage(named: "avatar"))
            }
            lblName.text = status?.user?.screen_name
            lblTime.text = status?.created_at
            lblSource.text = status?.source
            lblContent.text = status?.text
            
            memberShipView.image = status?.user?.mbRankImg
            verifiedView.image = status?.user?.verifiedImg
            
            //把图片数组传给pictureview, 让它自己计算frame
            picView.picURLs = status?.storePicURLs
            
            //当没有图片的时候 pictureview 跟底部的间距约束要去除
//            snp_updateConstraints: 前提是参照的view和参照的约束没有变化才能updateConstraints
            picView.snp_updateConstraints { (make) in
                let margin = status?.storePicURLs == nil ? 0 : GKStatusCellMargin
//                GKPrint("间距是-------\(status?.storePicURLs?)")
                make.top.equalTo(lblContent.snp_bottom).offset(margin)
            }
        }
        
    }
    
    
    //懒加载控件
    //头像
    private lazy var iconView: UIImageView = {
        let imgView = UIImageView(image: UIImage(named: "avatar"))
        imgView.layer.cornerRadius = 17.5
        imgView.layer.masksToBounds = true
        
        return imgView
    }()
    //用户名
    private lazy var lblName: UILabel = {
        let lbl = UILabel()
        lbl.font = UIFont.systemFontOfSize(14)
        lbl.textColor = UIColor.darkGrayColor()
        
        return lbl
    }()
    //微博时间
    private lazy var lblTime: UILabel = {
        let lbl = UILabel()
        lbl.font = UIFont.systemFontOfSize(11)
        lbl.textColor = UIColor.orangeColor()
        
        return lbl
    }()
    //发微博来源设备
    private lazy var lblSource: UILabel = {
        
        let lbl = UILabel()
        lbl.textColor = UIColor.lightGrayColor()
        lbl.font = UIFont.systemFontOfSize(11)
        
        return lbl
    }()
    
    //会员图标
    private lazy var memberShipView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_expired"))
    //认证图标
    private lazy var verifiedView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    //原创微博内容
    private lazy var lblContent: UILabel = {
        
        let lbl = UILabel()
        lbl.font = UIFont.systemFontOfSize(15)
        lbl.textColor = UIColor.darkGrayColor()
        
        let num = arc4random_uniform(10) % 2
        lbl.text = num == 0 ? "fasfadsfsdgdfgeargdfgddgergergfdsgdfgergdfgerdgdsdfghtsdhtr" : "发送的方式发生地方撒旦法师法第三方四的发送到发送到发送到给个跑狗克里斯丁和覅而更换合格如果二个IE人各工地开了个回复分解得更"
        
        lbl.sizeToFit()
        lbl.numberOfLines = 0
        
        return lbl
    }()
    
    //原创微博配图
    private lazy var  picView:GKStatusPictureView = GKStatusPictureView()
    
    
    
    
    
    

}
